﻿using System;
//using COC.Algorithm;
using COC.Algorithm;
using COC.Character.Extra;
using COC.Constant;
using COC.Debug;
using COC.Exception;
using COC.Management;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using COC.CEvent;
namespace COC.Service
{
    //cache game object when included in current scene ,or remove game object from cache list after destroy game object  
    public class CacheService : IService
    {
        private Dictionary<int, System.WeakReference> cacheGameObjects = new Dictionary<int, System.WeakReference>();

        protected internal CacheManager _cacheManager;


        public override void wireComponent()
        {
            DoStart();
            if (_cacheManager == null)
            {
                GameObject go = new GameObject(CommonConstants.AI_CACHE_POOL_NAME);
                GameObject.DontDestroyOnLoad(go);
                _cacheManager = new CacheManager(go);
            }
        }
      
        public GameObject spawnGameObject(string type, Transform parent, Vector3 pos, Vector3 scale, Quaternion rotation)
        {

            GameObject go=null;
            Component com=_cacheManager.prefabPool.Spawn(type);
            if (com != null)
            {
                if (com.gameObject!=null)
                    go = com.gameObject;
               
                    go.transform.localScale = scale;
                    go.transform.localPosition = pos;
                    go.transform.rotation = rotation;
                
            }
            return go;

        }
        void DoStart()
        {
            DebugTool.Log("I'm CacheService");
        }

        public bool isCachedGameObject(long modelId)
        {
            if (cacheGameObjects.ContainsKey((int)modelId))
            {
                WeakReference vWeakReference;
                cacheGameObjects.TryGetValue((int)modelId, out vWeakReference);
                if (vWeakReference != null && checked((long)vWeakReference.Target) != 0L)
                {
                    return true;
                }

            }
            return false;
        }







        public long getCachedGameObjectModelFlagByModelId(long modelId)
        {
            if (cacheGameObjects.Count > 0)
            {
                WeakReference vWeakReference;
                cacheGameObjects.TryGetValue((int)(modelId), out vWeakReference);
                if (vWeakReference != null)
                {
                    var _value = (checked((long)vWeakReference.Target));
                    if (_value != 0L)
                        return _value;
                }
            }
            
            return 0L;

        }

        /// <summary>
        /// execute cache event message 
        /// </summary>
        /// <param name="cEvent"></param>
        protected void Execute(CEventHandler cEvent)
        {
            if (cEvent.type == CEventType.CACHE_EVENT_ADD_OR_UPDATE_GAMEOBJECT_FLAG)
            {
                var _params = cEvent.obj as long[];

                if (_params != null && _params.Length == 2)
                {

                    var modelId = ((int)_params[0]);
                    var gameObjectId = cacheGameObjects[modelId];//根据模型类型获得游戏对象实例

                    cacheGameObjects.Add(modelId, new WeakReference(gameObjectId));


                }
                return;

            }


            if (cEvent.type == CEventType.CACHE_EVENT_REMOVE_GAMEOBJECT_FLAG)
            {

                var _params = cEvent.obj as long[];

                if (_params != null && _params.Length == 2)
                {
                    var modelId = ((int)_params[0]);

                    WeakReference vWeakReference;
                    cacheGameObjects.TryGetValue(modelId, out vWeakReference);
                    if (vWeakReference != null && checked((long)vWeakReference.Target) != 0L)
                        cacheGameObjects.Remove(modelId);

                    else
                    {
                        throw new CException("could not find this flag,maybe it has removed by garbage collection");
                    }

                }
                return;
            }
            {

            }
        }
    }
}